package com.seatiger.structure.queue;

import java.util.Random;

/**
 * 队列测试
 */
public class QueueMain {
    public static void main(String[] args) {
        int opCount = 100000;
        ArrayQueue arrayQueue = new ArrayQueue();
        double time1 = testQueue(arrayQueue, opCount);
        System.out.println("ArrayQueue, time:" + time1 + "s.");
        LoopQueue loopQueue = new LoopQueue();
        double time2 = testQueue(loopQueue, opCount);
        System.out.println("LoopQueue, time:" + time2 + "s.");
        LinkedListQueue linkedListQueue = new LinkedListQueue();
        double time3 = testQueue(linkedListQueue, opCount);
        System.out.println("LinkedListQueue, time:" + time3 + "s.");
    }

    private static double testQueue(Queue queue, int opCount) {
        long startTime = System.nanoTime();
        Random random = new Random();
        for (int i = 0; i < opCount; i++) {
            queue.enqueue(random.nextInt(Integer.MAX_VALUE));
        }
        for (int i = 0; i < opCount; i++) {
            queue.dequeue();
        }
        long endTime = System.nanoTime();
        return (endTime - startTime) / 1_000_000_000.0;
    }

    private static void testLoopQueue() {
        LoopQueue<Integer> loopQueue = new LoopQueue<>(4);
        for (int i = 0; i < 5; i++) {
            loopQueue.enqueue(i);
            System.out.println(loopQueue);
        }
        loopQueue.dequeue();
        System.out.println(loopQueue);
    }

    private static void testArrayQueue() {
        Queue<Integer> queue = new ArrayQueue<>(5);
        for (int i = 0; i < 5; i++) {
            queue.enqueue(i);
            System.out.println(queue);
        }
        queue.dequeue();
        System.out.println(queue);
    }

    private static void testLinkedListQueue() {
        Queue<Integer> queue = new LinkedListQueue<>();
        for (int i = 0; i < 5; i++) {
            queue.enqueue(i);
            System.out.println(queue);
        }
        queue.dequeue();
        System.out.println(queue);
    }
}
